#include "StdAfx.h"
#include "MenuBarItem.h"
#include "MenuBar.h"
#include "DrawGraphics.h"


CMenuBarItem::CMenuBarItem(LPCTSTR lpcsText)
: CMenuItem(lpcsText)
{
    m_pMenuBar = NULL;
    m_nWidth = 0;
}

CMenuBarItem::~CMenuBarItem(void)
{
}

int CMenuBarItem::CalcWidth()
{
    ASSERT(m_pMenuBar);
    SIZE sz;
    HFONT hFont = m_pMenuBar->GetFont();
    GRAPHICS::GetTextSizeEx(GetText(), hFont, NULL, &sz);
    m_nWidth = sz.cx + m_pMenuBar->GetTextOffset() * 2;
    return m_nWidth;
}


int CMenuBarItem::GetWidth()
{
    return m_nWidth;
}

int CMenuBarItem::GetHeight()
{
    return 0;
}

void CMenuBarItem::Draw( HDC hDc, CRect rcDraw )
{
    int nOldDc = ::SaveDC(hDc);
    ASSERT(m_pMenuBar);

    HBRUSH hBorderBrush = NULL;
    HBRUSH hBackBrush = NULL;
    COLORREF crText = 0x000000;

    if (m_pMenuBar->GetSelMenu() == this)
    {
        if (m_pMenuBar->IsShowMenu())
        {
            hBorderBrush = m_pMenuBar->GetSelectBorderBrush();
            hBackBrush = m_pMenuBar->GetSelectItemBackBrush();
            crText = m_pMenuBar->GetSelectTextColor();
        }
        else
        {
            hBorderBrush = m_pMenuBar->GetActivateBorderBrush();
            hBackBrush = m_pMenuBar->GetActivateItemBackBrush();
            crText = m_pMenuBar->GetActivateTextColor();
        }
    }
    else
    {
        hBorderBrush = m_pMenuBar->GetDefaultBorderBrush();
        hBackBrush = m_pMenuBar->GetDefaultItemBackBrush();
        crText = m_pMenuBar->GetDefaultTextColor();
    }

    ::SetTextColor(hDc, crText);
    ::FillRect(hDc, rcDraw, hBackBrush);
    ::FrameRect(hDc, rcDraw, hBorderBrush); 

    CRect rcText = rcDraw;
    rcText.left += m_pMenuBar->GetTextOffset();
    rcText.right -= m_pMenuBar->GetTextOffset();
    ::SetBkMode(hDc, TRANSPARENT);
    ::SelectObject(hDc, m_pMenuBar->GetFont());
    ::DrawText(hDc, GetText(), -1, rcText, DT_LEFT|DT_VCENTER|DT_SINGLELINE);
    ::RestoreDC(hDc, nOldDc);
}

void CMenuBarItem::SetChildMenu( CMenuEx * pMenu )
{
    ASSERT(m_pChildMenu == NULL);
    m_pChildMenu = pMenu;
}

void CMenuBarItem::SetOwnerMenu( CMenuBar * pMenuBar )
{
    m_pMenuBar = pMenuBar;
}

CMenuBar * CMenuBarItem::GetOwnerMenu()
{
    return m_pMenuBar;
}

